package com.jason.pulltorefreshandstickylistheaders;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.jason.pulltorefreshandstickylistheaders.adapter.PullTorefreshStickyAdapter;
import com.jason.pulltorefreshandstickylistheaders.view.PullToRefreshStickyListView;

import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

import static android.R.id.list;

public class MainActivity extends AppCompatActivity {

    private PullToRefreshStickyListView listview;
    private StickyListHeadersListView mStickyListHeadersListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (PullToRefreshStickyListView) findViewById(R.id.listview);
        listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<StickyListHeadersListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<StickyListHeadersListView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        listview.onRefreshComplete();
                    }
                },2000);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<StickyListHeadersListView> refreshView) {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        listview.onRefreshComplete();
                    }
                }, 2000);
            }
        });

        mStickyListHeadersListView = listview.getRefreshableView();
        mStickyListHeadersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), "Item " + position + " clicked!", Toast.LENGTH_SHORT).show();
            }
        });

        mStickyListHeadersListView.setOnHeaderClickListener(new StickyListHeadersListView.OnHeaderClickListener() {
            @Override
            public void onHeaderClick(StickyListHeadersListView l, View header, int itemPosition, long headerId, boolean currentlySticky) {
                Toast.makeText(getApplicationContext(), "Header " + headerId + " currentlySticky ? " + currentlySticky, Toast.LENGTH_SHORT).show();
            }
        });

        mStickyListHeadersListView.setOnStickyHeaderChangedListener(new StickyListHeadersListView.OnStickyHeaderChangedListener() {
            @Override
            public void onStickyHeaderChanged(StickyListHeadersListView l, View header, int itemPosition, long headerId) {

            }
        });

        mStickyListHeadersListView.setOnStickyHeaderOffsetChangedListener(new StickyListHeadersListView.OnStickyHeaderOffsetChangedListener() {
            @Override
            public void onStickyHeaderOffsetChanged(StickyListHeadersListView l, View header, int offset) {

            }
        });

        mStickyListHeadersListView.setDrawingListUnderStickyHeader(true);
        mStickyListHeadersListView.setAreHeadersSticky(true);
        mStickyListHeadersListView.setAdapter(new PullTorefreshStickyAdapter(this));



    }
}
